package com.baitengsoft.patentapimonitor.api.controller;


import com.baitengsoft.patentapimonitor.api.entity.WebApplicanttypeRlatEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebRolesEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.WebPlatformViewModel;
import com.baitengsoft.patentapimonitor.api.model.param.PlatformParam;
import com.baitengsoft.patentapimonitor.api.model.param.PlatformViewParam;
import com.baitengsoft.patentapimonitor.api.service.*;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *platform
 * @author
 * @date
 */
@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-平台管理",description = "平台管理")
public class AdminPlatformController extends AbstractController {

    @Autowired
    private WebRolesService webRolesService;
    @Autowired
    private CfgAreaService cfgAreaService;
    @Autowired
    private WebPlatformService webPlatformService;
    /**
     * 获取栏目样式
     */
    @ApiOperation(value = "栏目样式列表")
    @RequestMapping(value="/columnstyle", method= RequestMethod.GET)
    public Res getColumnStyle(){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<WebRolesEntity> entities=webRolesService.searchModels();
            if(entities!=null){
                flag=true;
                map.put("data",entities);
            }else {
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "省列表")
    @RequestMapping(value="/province", method= RequestMethod.GET)
    public Res getAreaProvince(){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        List<String> provinces=cfgAreaService.getProvince();
        if(provinces!=null){
            flag=true;
            map.put("data",provinces);
        }else{
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
            return Res.ok(map);
    }

    /**
     * 根据传入的省，查询其下面的市
     * @return
     */
    @ApiOperation(value = "市列表")
    @RequestMapping(value="/city", method= RequestMethod.GET)
    public Res getAreaCity(@RequestParam("province") String province){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        List<String> citys=cfgAreaService.getCity(province);
        if(citys!=null){
            flag=true;
            map.put("data",citys);
        }else{
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    /**
     * 根据传入的市，查询其下面的区
     * @param city
     * @return
     */
    @ApiOperation(value = "区列表")
    @RequestMapping(value="/place", method= RequestMethod.GET)
    public Res getAreaPlace(@RequestParam("city") String city){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        List<String> places=cfgAreaService.getPlace(city);
        if(places!=null){
            flag=true;
            map.put("data",places);
        }else{
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }



    @ApiOperation(value = "获取平台信息列表")
    @RequestMapping(value="/searchplatforms", method= RequestMethod.POST)
    public Res searchPlatforms(@RequestBody PlatformParam param){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<WebPlatformViewModel> pageResult=webPlatformService.searchPlatforms(param);
            if(pageResult!=null){
                flag=true;
                map.put("data",pageResult);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    /**
     * ID为0时新增，大于0时编辑
     * @param param
     * @return
     */
    @ApiOperation(value = "编辑平台信息")
    @RequestMapping(value="/editplatforms", method= RequestMethod.POST)
    public Res editPlatforms(@RequestBody PlatformViewParam param){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(String.valueOf(getAdminUserId()));
            Integer successId=webPlatformService.saveOrUpdatePlatformWrap(param,userId);

            if(successId==1){
                flag=true;
                map.put("msg","操作成功");
            }else{
                map.put("msg","操作失败");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "删除平台信息")
    @RequestMapping(value="/delplatforms", method= RequestMethod.GET)
    public Res delPlatforms(@RequestParam("idStr") String idStr){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(String.valueOf(getAdminUserId()));//暂时先不考虑登录问题
            Integer successId=webPlatformService.delPlatformById(idStr,userId);
            if(successId==1){
                flag=true;
                map.put("msg","删除成功");
            }else{
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            map.put("msg","删除异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "启用平台信息")
    @RequestMapping(value="/startplatforms", method= RequestMethod.GET)
    public Res startPlatforms(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(String.valueOf(getAdminUserId()));//暂时先不考虑登录问题
            Integer successId=webPlatformService.setStartDateById(id,userId);
            if(successId==1){
                flag=true;
                map.put("msg","启用成功");
            }else{
                map.put("msg","启用失败");
            }
        } catch (Exception e) {
            map.put("msg","启用异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "禁用平台信息")
    @RequestMapping(value="/disabledplatforms", method= RequestMethod.GET)
    public Res disabledPlatforms(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(String.valueOf(getAdminUserId()));//暂时先不考虑登录问题
            Integer successId=webPlatformService.setDisabledById(id,userId);
            if(successId==1){
                flag=true;
                map.put("msg","禁用成功");
            }else{
                map.put("msg","禁用失败");
            }
        } catch (Exception e) {
            map.put("msg","禁用异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "试用平台信息")
    @RequestMapping(value="/tryoutplatforms", method= RequestMethod.GET)
    public Res tryOutPlatforms(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer userId=Integer.parseInt(String.valueOf(getAdminUserId()));//暂时先不考虑登录问题
            Integer successId=webPlatformService.setTryById(id,userId);
            if(successId==1){
                flag=true;
                map.put("msg","试用成功");
            }else{
                map.put("msg","试用失败");
            }
        } catch (Exception e) {
            map.put("msg","试用异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "获取平台信息")
    @RequestMapping(value="/searchsingleplatforms", method= RequestMethod.GET)
    public Res searchSinglePlatforms(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            WebPlatformEntity entity=webPlatformService.searchById(id);
            if(entity!=null){
                flag=true;
                map.put("data",entity);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取信息异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "验证平台名称")
    @RequestMapping(value="/checkPlatformName", method= RequestMethod.GET)
    public Res checkPlatformName(@RequestParam("platformName") String platformName){
        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Boolean resultFlag=webPlatformService.chekPlatformName(platformName);
            if(resultFlag){
                flag=true;
                map.put("msg","该名称可用");
            }else{
                map.put("msg","该名称不可用");
            }
        } catch (Exception e) {
            map.put("msg","验证异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "检查初始化数据，区域未设置返回code:420 AREA_UNINIT，指标未设置421 INDICATOR_UNINIT")
    @PostMapping(value = "/checkInit")
    public Res checkInit(@RequestParam(value = "platformId") int platformId) {

        return webPlatformService.checkInit(platformId);
    }

}
